<?php
/**
 * 应用类，一切由此开始
 */
namespace Hi;

class App {

    /**
     * 控制器
     * @var
     */
    public $controller;

    /**
     * 动作
     * @var
     */
    public $action;

    /**
     * 函数库清单数组
     * @var array
     */
    private static $_funcs = array();

    /**
     * Router实例
     * @var
     */
    protected $_router;

    /**
     * Params实例
     * @var
     */
    protected $_params;

    /**
     * Request实例
     * @var
     */
    protected $_request;

    /**
     * App实例
     * @var App
     */
    protected static $_instance;

    /**
     * 数据库对象连接池
     * @var
     */
    private $_dbPool;
    /**
     * 获取实例
     * @return App
     */
    public static function getInstance() {
        if (self::$_instance === NULL) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * App constructor.
     */
    protected function __construct() {
        $this->_params = Params::getInstance();     //参数处理
        $this->getRouter();

        $this->_request = Request::getInstance();  //请求处理
    }

    /**
     * 开始
     */
    public function run() {
        $requestParams = $this->_request->parseRequestParams($this->_router->getRouteType());
        if (!empty($requestParams)) {
            $this->_params->setParams($requestParams);  //设置请求参数
        }

        $this->loadFunc('Global');           //加载函数库

        if (DB_AUTO_CONNECT === true) {     //初始化数据库

        }
        $this->dispatch();                  //调派
    }

    /**
     * 获取路由器对象
     * @return Router
     */
    public function getRouter() {
        if ($this->_router === NULL) {
            $this->_router = new Router();
        }
        return $this->_router;
    }

    /**
     * 加载函数库
     * @param $func
     * @return bool
     * @throws Exceptions
     */
    private function loadFunc($func) {
        $file = HI_PATH . DS . 'Functions' . DS . ucfirst($func) . '.func.php';
        $key = md5($file);
        if (!isset(self::$_funcs[$key])) {
            if (file_exists($file)) {
                include ($file);
                self::$_funcs[$key] = true;
            } else {
                throw new Exceptions('Function "' . $func . '" not found.');
            }
        }
        return true;
    }

    /**
     * 调派
     */
    public function dispatch() {
        $request = Request::getInstance();           //实例化请求处理类
        $this->controller = $request->_controller;   //控制器名
        $this->action = $request->_action;           //方法名

        $controllerName = ucfirst($this->controller);//控制器转大写
        $isApi = (REST_ON === true && $controllerName == 'Api') ? true : false;  //判断是否开启Restful模式

        if ($isApi === true) {      //Restful模式

        } else {
            //1、控制器文件路径
            $controllerFile = APP_PATH . DS . 'Controller' . DS . $controllerName . '.php';
            //2、如果控制器文件不存在，跑出异常
            if (!file_exists($controllerFile)) {
                throw new Exceptions('Controller file "' . $controllerFile . '" not found.',404);
            }

            //3、控制器名   App\Controller\Index
            $controllerName = APP_NAMESPACE . '\\Controller\\' . $controllerName;
            //4、如果控制器存在，实例化；不存在，抛出异常
            if (class_exists($controllerName)) {
                $controller = new $controllerName;
            } else {
                throw new Exceptions('Controller "' . $controllerName . '" does not exist ."', 404);
            }

            $action = $this->action .'Hi';

            //5、如果方法存在，执行方法；不存在，抛出异常
            if (method_exists($controller,$action)) {
                $controller->$action();
            } else {
                throw new Exceptions('Action "' . $this->action . '"does not exist.',404);
            }
        }

    }
}